//
//  AppDelegate.swift
//  微博
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        //判断是不是新的版本号
//        let version = saveVersion()
        //注册通知
        notifinecation()
        window?.rootViewController = selecetedRootViewVC()
//        window?.rootViewController = UINavigationController(rootViewController: AddPictureViewController())
        return true
    }
    
    //通过偏好设置来保存版本号
    func saveVersion()->Bool {
        //CFBundleShortVersionString
        
        let dict = NSBundle.mainBundle().infoDictionary
        let str = dict!["CFBundleShortVersionString"] as! String
        let nowVersion = Double(str)
        let defaults = NSUserDefaults.standardUserDefaults()
        let key = "version"
        let version =  defaults.doubleForKey(key)
        //保存当前版本号
        defaults.setDouble(nowVersion ?? 0, forKey: key)
        return nowVersion > version
    }
    
    //注册一个通知
    func notifinecation() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "setRootViewVC:", name:rootVC , object: nil)
    }
    
    //通知回调的方法
    //带有参数
    func setRootViewVC(notifine:NSNotification) {
        //判断参数是否为空
        if notifine.object != nil {
            //是授权页面
            //设置根控制器为欢迎页面
            window?.rootViewController = WelcomeViewController()
            return
        }
        
        window?.rootViewController = MainViewController()
    }
    //返回控制器
    func selecetedRootViewVC()->UIViewController {
        //判断是否登录
        if UserAccontViewMedol().userBase {
            //判断是否是新版本
            if saveVersion() {
                return NewFeatureController()
            }
            return WelcomeViewController()
        }
        //返回访客视图
        return MainViewController()
        
        
    }
    //回收通知
    deinit {
        //收回通知
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

